package com.junko.service.back.impl;

import com.junko.core.base.BaseServiceImpl;
import com.junko.dao.back.MenuDao;
import com.junko.model.back.Menu;
import com.junko.model.back.ZtreeNode;
import com.junko.service.back.IMenuService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuService extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> findTopParentsBySystemId(Integer systemId) {
        return menuDao.findTopParentsBySystemId(systemId);
    }

    @Override
    public List<Menu> findByParentId(Integer parentId) {
        return menuDao.findByParentId(parentId);
    }

    @Override
    public List<ZtreeNode> childNodeData(Integer systemId, Integer parentId){
        List<Menu> list;
        if(parentId!=null){
            list = menuDao.findByParentId(parentId);
        }else{
            list = menuDao.findTopParentsBySystemId(systemId);
        }
        List<ZtreeNode> nodeList = new ArrayList<ZtreeNode>();
        ZtreeNode node;
        for (Menu menu : list) {
            node = new ZtreeNode();
            node.setId(menu.getId()+"");
            node.setName(menu.getName());
            node.setIsParent(true);
            nodeList.add(node);
        }
        return nodeList;
    }

    @Override
    public Integer save(Integer pId, String name, int orderId){
        Menu pMenu = menuDao.selectByPrimaryKey(pId);
        menuDao.updateByPrimaryKeySelective(pMenu);

        Menu menu = new Menu();
        menu.setParentId(pId);
        menu.setOrderNo(orderId);
        menu.setName(name);
        menu.setSystemId(pMenu.getSystemId());
        menuDao.insertSelective(menu);
        return menu.getId();
    }

    @Override
    public boolean update(Integer id, Integer pId, String name){
        Menu menu = menuDao.selectByPrimaryKey(id);
        //更新上级模块
        if(pId!=null){
            menu.setParentId(pId);
        }
        if(StringUtils.isNotBlank(name)){
            menu.setName(name);
        }
        return menuDao.updateByPrimaryKeySelective(menu)==1;
    }

    @Override
    public boolean delete(Integer id) {
        // 是否存在子节点
        if(CollectionUtils.isNotEmpty(findByParentId(id))){
            return false;
        }
        // 删除
        return menuDao.deleteByPrimaryKey(id)==1;
    }

    @Override
    public boolean setOperator(Integer menuId, String operatorId){
        Menu menu = menuDao.selectByPrimaryKey(menuId);
        menu.setOperatorId(operatorId);
        return menuDao.updateByPrimaryKeySelective(menu)==1;
    }

    @Override
    public boolean rename(Integer id, String name) {
        Menu menu = menuDao.selectByPrimaryKey(id);
        if(StringUtils.isNotBlank(name)){
            menu.setName(name);
        }
        return menuDao.updateByPrimaryKeySelective(menu)==1;
    }
}